#ifdef KITSCHY_DEBUG_MEMORY 
#include "debug_memorymanager.h"
#endif

#ifdef _WIN32
#include <windows.h>
#include <windowsx.h>
#else
#include <sys/time.h>
#include <time.h>
#include "unistd.h"
#endif

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"

#include <GL/gl.h>
#include <GL/glu.h>
#include <FL/glut.H>

#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/Fl_Button.H>

#include "BList.h"
#include "BVector.h"
#include "quaternion.h"
#include "symbol.h"
#include "sort.h"
#include "ontology.h"
#include "featureterm.h"
#include "FTKBase.h"

#include "myglutaux.h"
#include "CMC.h"
#include "3dobject.h"
#include "AWLocalViewport.h"

#include "ode/ode.h"

#include "AWEntity.h"
#include "AWMessage.h"
#include "AWScenario.h"
#include "AWBody.h"
#include "AWSensor.h"
#include "AWBrain.h"
#include "AWAgent.h"
#include "AWWorld.h"

#include "AwApp.h"
#include "loader.h"

#include "awwindow.h"
#include "awbwindow.h"


long current_time=0,last_time=0;
float TIME_INC=0.01F,MAX_TIME_INC=0.1F;
float TIME_RATE=1.0F;

int STARTX=20,STARTY=60;
int SCREEN_DX=1024,SCREEN_DY=768;
//int SX=800,SY=600;
//int SX=640,SY=480;
//int SX=512,SY=384;
//int SX=320,SY=240;

AWWindow *ww=0;
float world_time=0.0;
int redraw_FPS=50;
int last_redraw,redraw_period=(redraw_FPS>0 ? 1000/redraw_FPS:0);
bool active=false;
bool running=true;

// Application data: 
AW_World *World=0;


int main(int argc,char **argv)
{
	AWCWindow *cw = new AWCWindow(STARTX,STARTY);
	
	AW_Entity::set_debug_report();

	ww = new AWWindow(STARTX+150,STARTY,SCREEN_DX,SCREEN_DY);
	ww->resizable(ww);

	Fl::visual(FL_RGB);
	cw->show(argc,argv);
	ww->show(argc,argv);

	last_redraw=last_time=GetTickCount();

	{
		// Load the world: 
		FILE *fp=fopen(AW_INITIAL_WORLD,"r");
		if (fp!=0) {
			if (World!=0) delete World;
			World=(AW_World *)ClassLoader(fp,0,0);
			world_time=World->GetActTime();
			MAX_TIME_INC=World->GetMaxTimeInc();
			TIME_RATE=World->GetTimeRate();
			fclose(fp);
		} /* if */ 
	}

	while (running &&
		   Fl::check()) {
		if (World!=0) {
			current_time=GetTickCount();
			if (active) {
				TIME_INC=(TIME_RATE*float(current_time-last_time))/1000.0F;
				if (TIME_INC>MAX_TIME_INC) TIME_INC=MAX_TIME_INC;
				if (TIME_INC!=0) {
					World->actualize(world_time,TIME_INC);
					world_time+=TIME_INC;
					last_time=current_time;
				} /* if */ 
			} else {
				last_time=current_time;
			} /* if */ 

			if (current_time>last_redraw+redraw_period) {
				last_redraw=current_time;
				ww->redraw();
			} /* if */ 

			// If the simulation is not running, then spare some cycles with the OS:
			if (!active) {
#ifdef WIN32
				Sleep(1);
#else
				usleep(1);
#endif
			} /* if */ 
		} else {
			last_time=GetTickCount();
		} /* if */ 
	} /* while */ 

	delete cw;
	delete ww;

	dCloseODE();
	delete World;
	World=0;

	Symbol::reset();

	return 0;
} /* main */ 


